/*
 * Description: description of material.js
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Dongxu.Ni
 * Date: 2020/4/15 0:29
 */

import { materialList } from '../../api/quote/material'

const state = {
  materialCollects: []
}

const getters = {
  getMaterialCollects: state => state.materialCollects
}

const mutations = {
  SET_MATERIAL_COLLECTS: (state, materialCollects) => {
    state.materialCollects = materialCollects
  }
}

const actions = {
  initMaterialCollectsLazy({ commit, state }) {
    return new Promise((resolve, reject) => {
      if (state.materialCollects.length > 0) {
        resolve()
      } else {
        materialList({})
          .then(response => {
            commit('SET_MATERIAL_COLLECTS', response.data)
            resolve()
          })
          .catch(err => reject(err))
      }
    })
  },
  initMaterialCollects({ commit }) {
    return new Promise((resolve, reject) => {
      materialList({})
        .then(response => {
          commit('SET_MATERIAL_COLLECTS', response.data)
          resolve()
        })
        .catch(err => reject(err))
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
